/* Example of embedding Python in another program */

#include "python2.5/Python.h"

void initxyzzy(void); /* Forward */
int readfile(char* file, char* funcname);

main(int argc, char **argv)
{
	/* Pass argv[0] to the Python interpreter */
	Py_SetProgramName(argv[0]);

	/* Initialize the Python interpreter.  Required. */
	Py_Initialize();

	/* Add a static module */
	initxyzzy();

	/* Define sys.argv.  It is up to the application if you
	   want this; you can also let it undefined (since the Python 
	   code is generally not a main program it has no business
	   touching sys.argv...) */
	PySys_SetArgv(argc, argv);

	/* Do some application specific code */
	printf("Hello, brave new world\n\n");

	/* Execute some Python statements (in module __main__) */
	PyRun_SimpleString("import sys\n");
	PyRun_SimpleString("print sys.builtin_module_names\n");
	PyRun_SimpleString("print sys.modules.keys()\n");
	PyRun_SimpleString("print sys.executable\n");
	PyRun_SimpleString("print sys.argv\n");

	PyRun_SimpleString("import xyzzy\n");
	PyRun_SimpleString("print xyzzy.foo()\n");
	readfile("test_embed", "multiply_twice");
	/* Note that you can call any public function of the Python
	   interpreter here, e.g. call_object(). */

	Py_Finalize();
	/* Some more application specific code */
	printf("\nGoodbye, cruel world\n");

}

/* A static module */

/* 'self' is not used */
static PyObject *
xyzzy_foo(PyObject *self, PyObject* args)
{
	return PyInt_FromLong(42L);
}

static PyMethodDef xyzzy_methods[] = {
	{"foo",		xyzzy_foo,	METH_NOARGS,
	 "Return the meaning of everything."},
	{NULL,		NULL}		/* sentinel */
};

void
initxyzzy(void)
{
	PyImport_AddModule("xyzzy");
	Py_InitModule("xyzzy", xyzzy_methods);
}

int readfile(char* file, char* funcname)
{
	PyObject *pName, *pModule, *pDict, *pFunc;
	PyObject *pArgs, *pValue;
	int i;

	pName = PyString_FromString(file);
	/* Error checking of pName left out */

	pModule = PyImport_Import(pName);
	Py_DECREF(pName);

	if (pModule != NULL) {
		pFunc = PyObject_GetAttrString(pModule, funcname);
		/* pFunc is a new reference */

		if (pFunc && PyCallable_Check(pFunc)) {
			pArgs = PyTuple_New(2);
			PyTuple_SetItem(pArgs, 0, PyInt_FromLong(8));
			PyTuple_SetItem(pArgs, 1, PyInt_FromLong(6));
			/*for (i = 0; i < argc - 3; ++i) {
					pValue = PyInt_FromLong(atoi(argv[i + 3]));
					if (!pValue) {
							Py_DECREF(pArgs);
							Py_DECREF(pModule);
							fprintf(stderr, "Cannot convert argument\n");
							return 1;
					}
					// pValue reference stolen here: 
			}*/
			pValue = PyObject_CallObject(pFunc, pArgs);
			Py_DECREF(pArgs);
			if (pValue != NULL) {
				printf("Result of call: %ld\n", PyInt_AsLong(pValue));
				Py_DECREF(pValue);
			}
			else {
				Py_DECREF(pFunc);
				Py_DECREF(pModule);
				PyErr_Print();
				fprintf(stderr,"Call failed\n");
				return 1;
			}
		}
		else {
			if (PyErr_Occurred())
				PyErr_Print();
			fprintf(stderr, "Cannot find function \"%s\"\n", funcname);
		}
		Py_XDECREF(pFunc);
		Py_DECREF(pModule);
	}
	else {
		PyErr_Print();
		fprintf(stderr, "Failed to load \"%s\"\n", file);
		return 1;
	}
	return 0;
}
